home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / streambuf.h < prev   
C/C++ Source or Header  |  1992-01-17  |  4KB  |  163 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _streambuf_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #define _streambuf_h 1
  24.  
  25. /* streambufs. 
  26.    basic streambufs and filebufs are as in Stroustrup, ch 8,
  27.    but the base class contains virtual extensions that allow
  28.    most capabilities of libg++ Files to be used as streambufs
  29.    via `Filebufs'.
  30. */
  31.  
  32. #include <stdio.h>
  33. #include <builtin.h>
  34. #include <Fmodes.h>
  35.  
  36. // Flag used to distinguish old streams from new iostreams.
  37. #define _OLD_STREAMS
  38.  
  39. // see below for NO_LINE_BUFFER_STREAMBUFS
  40.  
  41. #ifndef BUFSIZE
  42. #ifdef BUFSIZ
  43. #define BUFSIZE BUFSIZ
  44. #else
  45. #define BUFSIZE 1024
  46. #endif
  47. #endif
  48.  
  49. enum open_mode // filebuf open modes
  50.   input=0, 
  51.   output=1, 
  52.   append=2 
  53. }; 
  54.  
  55. class streambuf
  56. {
  57. public:
  58.   char*       base;          // start of buffer
  59.   char*       pptr;          // put-pointer (and gptr fence)
  60.   char*       gptr;          // get-pointer
  61.   char*       eptr;          // last valid addr in buffer
  62.  
  63.   char        alloc;         // true if we own freestore alloced buffer
  64.  
  65.               streambuf();
  66.               streambuf(char* buf, int buflen);
  67.  
  68.   virtual    ~streambuf();
  69.  
  70.   int         doallocate();
  71.   int         allocate();
  72.  
  73.  
  74.   int         must_overflow(int ch); // true if should call overflow
  75.  
  76.   virtual int overflow(int c = EOF); // flush -- return EOF if fail
  77.   virtual int underflow();           // fill -- return EOF if fail
  78.  
  79.   int         sgetc();          // get one char (as int) or EOF
  80.   int         snextc();         // get and advance
  81.   void        stossc();         // advance only
  82.  
  83.   int         sputbackc(char);   // unget
  84.  
  85.   int         sputc(int c = EOF); // write one char
  86.  
  87.   virtual streambuf*  setbuf(char* buf, int buflen, int preloaded_count = 0);
  88.                                 // (not virtual in AT&T)
  89.  
  90. // the following aren't in AT&T version:
  91.  
  92.   int         sputs(const char* s);           // write null-terminated str
  93.   int         sputsn(const char* s, int len); // write len bytes
  94.  
  95.   virtual const char* name();
  96.  
  97.  
  98.   virtual streambuf*  open(const char* name, open_mode m);
  99.   virtual streambuf*  open(const char* filename, io_mode m, access_mode a);
  100.   virtual streambuf*  open(const char* filename, const char* m);
  101.   virtual streambuf*  open(int  filedesc, io_mode m);
  102.   virtual streambuf*  open(FILE* fileptr);
  103.  
  104.   virtual int         is_open();
  105.   virtual int         close();
  106.  
  107.   virtual void        error();
  108. };
  109.  
  110.  
  111. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  112.  
  113.  
  114. inline int streambuf::must_overflow(int ch)
  115. {
  116. #ifndef NO_LINE_BUFFER_STREAMBUF
  117.   return pptr >= eptr || ch == '\n';
  118. #else
  119.   return pptr >= eptr;
  120. #endif
  121. }
  122.  
  123.  
  124. inline int streambuf::allocate()
  125. {
  126.   return (base == 0)? doallocate() : 0; 
  127. }
  128.  
  129. inline int streambuf::sgetc()
  130. {
  131.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  132. }
  133.  
  134.  
  135. inline int streambuf::snextc()
  136. {
  137.   ++gptr;
  138.   return (gptr >= pptr)? underflow() : int((unsigned char)(*gptr));
  139. }
  140.  
  141.  
  142. inline void streambuf::stossc()
  143. {
  144.   if (gptr >= pptr) underflow(); else gptr++;
  145. }
  146.  
  147.  
  148. inline int streambuf::sputbackc(char ch)
  149. {
  150.   return (gptr > base)? int((unsigned char)(*--gptr = ch)) : EOF;
  151. }
  152.  
  153. inline int streambuf::sputc(int ch)
  154. {
  155.   return must_overflow(ch)? overflow(ch) : 
  156.                             int((unsigned char)(*pptr++ = char(ch)));
  157. }
  158.  
  159. #endif
  160.  
  161. #endif
  162.